<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller\v1;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Dao\UserDao;
use App\Dao\UserRelationDao;
use App\Service\PhoneService;
use App\Service\UserRelationService;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Context;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class User extends AbstractController
{
    /**
     * @Inject
     * @var UserDao
     */
    protected $userDao;

    /**
     * @Inject
     * @var UserService
     */
    protected $userService;

    /**
     * @Inject
     * @var PhoneService
     */
    protected $phoneService;

    /**
     * @Inject
     * @var UserRelationDao
     */
    protected $userRelationDao;

    /**
     * @Inject
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject
     * @var UserRelationService
     */
    protected $userRelationService;

    /**
     * 获取用户信息.
     */
    public function getUserInfo(): \Psr\Http\Message\ResponseInterface
    {
        $userId = Context::get('user_id');
        $userInfo = $this->userService->getUserInfo($userId);
        return $this->success($userInfo);
    }

    /**
     * 增加一个关注.
     */
    public function addFollow(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $validate = $this->validationFactory->make(
            $data,
            [
                'user_id' => 'required|check_user_isset',
            ],
            [
                'user_id.required' => '请选择要关注的用户',
            ]
        );
        if ($validate->fails()) {
            $msg = $validate->errors()->first();
            return $this->success([], ErrorCode::PARAM_ERROR, $msg);
        }
        $userId = Context::get('user_id');
        if ($this->userRelationDao->checkRelationIsset($userId, $data['user_id'])) {
            return $this->success();
        }
        $ret = $this->userRelationDao->addFollow($userId, $data['user_id']);
        return $this->success();
    }

    /**
     * 取消关注.
     */
    public function cancelFollow(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $validate = $this->validationFactory->make(
            $data,
            [
                'user_id' => 'required|check_user_isset',
            ],
            [
                'user_id.required' => '请选择要操作的用户',
            ]
        );
        if ($validate->fails()) {
            $msg = $validate->errors()->first();
            return $this->success([], ErrorCode::PARAM_ERROR, $msg);
        }
        $userId = Context::get('user_id');
        if (! $this->userRelationDao->checkRelationIsset($userId, $data['user_id'])) {
            return $this->success();
        }
        $ret = $this->userRelationDao->removeFollow($userId, $data['user_id']);
        return $this->success();
    }

    /**
     * 拉黑.
     */
    public function addBlock(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $validate = $this->validationFactory->make(
            $data,
            [
                'user_id' => 'required|check_user_isset',
            ],
            [
                'user_id.required' => '请选择要拉黑的用户',
            ]
        );
        if ($validate->fails()) {
            $msg = $validate->errors()->first();
            return $this->success([], ErrorCode::PARAM_ERROR, $msg);
        }
        $userId = Context::get('user_id');
        if ($this->userRelationDao->checkRelationIsset($userId, $data['user_id'], 2)) {
            return $this->success();
        }
        $ret = $this->userRelationDao->addBlock($userId, $data['user_id']);
        return $this->success();
    }

    /**
     * 取消拉黑.
     */
    public function cancelBlock(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $validate = $this->validationFactory->make(
            $data,
            [
                'user_id' => 'required|check_user_isset',
            ],
            [
                'user_id.required' => '请选择要操作的用户',
            ]
        );
        if ($validate->fails()) {
            $msg = $validate->errors()->first();
            return $this->success([], ErrorCode::PARAM_ERROR, $msg);
        }
        $userId = Context::get('user_id');
        if (! $this->userRelationDao->checkRelationIsset($userId, $data['user_id'], 2)) {
            return $this->success();
        }
        $ret = $this->userRelationDao->removeBlock($userId, $data['user_id']);
        return $this->success();
    }

    /**
     * 获取关注列表.
     */
    public function getFollowList(): \Psr\Http\Message\ResponseInterface
    {
        $userId = Context::get('user_id');
        $list = $this->userService->getFollowList($userId, 0, 10);
        return $this->success($list);
    }

    /**
     * 获取被关注列表.
     */
    public function getFollowedList(): \Psr\Http\Message\ResponseInterface
    {
        $userId = Context::get('user_id');
        $list = $this->userService->getFollowedList($userId, 0, 10);
        return $this->success($list);
    }
}
